import { _decorator, Component, Enum, error, EventTouch, find, instantiate, Label, Node, Prefab } from 'cc';
import { EventManager, MyEvent } from '../AshFramework/Managers/EventManager';
import { ITEM } from '../AshFramework/Datas/Constant';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { Backpack } from './Backpack';
import { GameData } from './GameData';
import { Magazine } from './Magazine';
import { ResourceUtil } from '../AshFramework/Utils/ResourceUtil';
import { GameManager } from './GameManager';
import { HintController } from './HintController';
import { Bullet } from './Bullet';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { TipsController } from './TipsController';
const { ccclass, property } = _decorator;

@ccclass('Item')
export class Item extends Component {

    @property({ type: Enum(ITEM) })
    ItemType: ITEM = ITEM.WEAPON;

    @property({ type: Boolean, displayName: "是否有弹匣" })
    HaveMagazine: boolean = false;

    @property({ type: Boolean, displayName: "是否是新建弹匣" })
    IsNewMagazine: boolean = false;

    Name: string = "";
    IsChecked: boolean = false;
    Checked: Node = null;

    private _type: string;
    private _isClick: boolean = false;

    protected onLoad(): void {
        this.Checked = this.node.getChildByName("选中框");
        this.node.on(Node.EventType.TOUCH_END, this.onTouchend, this);
        if (this.IsNewMagazine) {
            return;
        }
        if (this.ItemType == ITEM.WEAPON) {
            EventManager.on(MyEvent.WEAPON_UNCHECKED, this.uncheckedWeapon, this);
        } else if (this.ItemType == ITEM.MAGAZINE) {
            EventManager.on(MyEvent.MAGAZINE_UNCHECKED, this.uncheckedMagazine, this);
        } else if (this.ItemType == ITEM.BULLET) {
            EventManager.on(MyEvent.BULLET_UNCHECKED, this.uncheckedBullet, this);
        }
    }

    protected start(): void {
        if (this.ButtonNode) {
            this.ButtonNode.getChildByName("加1").on(Node.EventType.TOUCH_END, this.addBullet, this);
            this.ButtonNode.getChildByName("减1").on(Node.EventType.TOUCH_END, this.subBullet, this);
            this.ButtonNode.getChildByName("装满").on(Node.EventType.TOUCH_END, this.fillBullet, this);
        }
    }

    show(checked: boolean) {
        this.IsChecked = true;
        this.Checked.active = checked;
    }

    showMagazine() {
        this.IsChecked = true;
        this.Checked.active = true;
        this.ButtonNode.active = true;
    }

    onTouchend(event: EventTouch) {
        AudioManager.PlaySound(Audios.ButtonClick);
        if (TipsController.Instance.IsPack && this.node.name == "背包_武器_自动手枪" && !this._isClick) {
            this._isClick = true;
            TipsController.Instance.nextTips2D(1);
        }

        if (this.IsNewMagazine) {
            Backpack.Instance.newMagazine();
            if (TipsController.Instance.IsPack && !this._isClick) {
                this._isClick = true;
                console.error(1)
                TipsController.Instance.nextTips2D(2);
            }
        } else if (!this.IsChecked) {
            if (this.ItemType == ITEM.WEAPON) {
                EventManager.Scene.emit(MyEvent.WEAPON_UNCHECKED);
                this.IsChecked = true;
                this.Checked.active = true;
                Backpack.Instance.switchWeapon(this);
            } else if (this.ItemType == ITEM.MAGAZINE) {
                EventManager.Scene.emit(MyEvent.MAGAZINE_UNCHECKED);
                this.IsChecked = true;
                this.Checked.active = true;
                this.ButtonNode.active = true;
            } else if (this.ItemType == ITEM.BULLET) {
                EventManager.Scene.emit(MyEvent.BULLET_UNCHECKED);
                this.IsChecked = true;
                this.Checked.active = true;
                Backpack.Instance.CheckedBullet = this;
            }
        }
    }

    uncheckedWeapon() {
        AudioManager.PlaySound(Audios.ButtonClick);
        this.Checked.active = false;
        this.IsChecked = false;
    }

    uncheckedMagazine() {
        AudioManager.PlaySound(Audios.ButtonClick);
        this.Checked.active = false;
        this.IsChecked = false;
        this.ButtonNode.active = false;
    }

    uncheckedBullet() {
        AudioManager.PlaySound(Audios.ButtonClick);
        this.Checked.active = false;
        this.IsChecked = false;
    }

    //#region 弹匣包
    @property(Label)
    HaveLabel: Label = null;

    @property(Label)
    TotalLabel: Label = null;

    @property(Node)
    ButtonNode: Node = null;

    private _have: number = 0;
    private _total: number = 0;
    private _addNode: Node = null;
    private _subNode: Node = null;
    private _fillNode: Node = null;

    private _magazine: Magazine = null;

    // showMagazine(haveNumber: number, totalLabel: number) {
    //     this._have = haveNumber;
    //     this._total = totalLabel;
    //     this.HaveLabel.string = this._have.toString();
    //     this.TotalLabel.string = this._total.toString();
    // }

    addMagazine(magazine: Magazine) {
        this._magazine = magazine;
        this.showMagazineData();
    }

    showMagazineData() {
        this.HaveLabel.string = this._magazine.Bullets.length.toString();
        this.TotalLabel.string = this._magazine.Capacity.toString();
    }

    addBullet(event: EventTouch) {
        AudioManager.PlaySound(Audios.ButtonClick);
        if (!Backpack.Instance.CheckedBullet) {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                const hint: string = `没有与之相匹配的子弹！`;
                node.getComponent(HintController).showHint(hint);
            })
        } else if (this._magazine.Bullets.length >= this._magazine.Capacity) {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                const hint: string = `弹匣已满！`;
                node.getComponent(HintController).showHint(hint);
            })
        } else {
            this._magazine.Bullets.push(Backpack.Instance.CheckedBullet.Name);
            Backpack.Instance.CheckedBullet.subNumBullet();
            GameData.DateSave();
            this.showMagazineData();
        }
    }

    subBullet(event: EventTouch) {
        AudioManager.PlaySound(Audios.ButtonClick);
        if (this._magazine.Bullets.length <= 0) {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                const hint: string = `弹匣中没有子弹！`;
                node.getComponent(HintController).showHint(hint);
            })
        } else {
            const name: string = this._magazine.Bullets.pop();
            GameData.addBulletByName(name);
            this.scheduleOnce(() => {
                Backpack.Instance.showBullet();
                GameData.DateSave();
                this.showMagazineData();
            })
        }
    }

    fillBullet(event: EventTouch) {
        AudioManager.PlaySound(Audios.ButtonClick);
        if (!Backpack.Instance.CheckedBullet) {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                const hint: string = `没有与之相匹配的子弹！`;
                node.getComponent(HintController).showHint(hint);
            })
        } else if (this._magazine.Bullets.length >= this._magazine.Capacity) {
            ResourceUtil.LoadPrefab("UI/Hint").then((prefab: Prefab) => {
                const node: Node = instantiate(prefab);
                node.parent = GameManager.Instance.Canvas;
                const hint: string = `弹匣已满！`;
                node.getComponent(HintController).showHint(hint);
            })
        } else {
            const need: number = this._magazine.Capacity - this._magazine.Bullets.length;
            const have: number = Backpack.Instance.CheckedBullet.HaveBulletCount;
            let count: number = need > have ? have : need;

            for (let index = 0; index < count; index++) {
                this._magazine.Bullets.push(Backpack.Instance.CheckedBullet.Name);
            }
            Backpack.Instance.CheckedBullet.subNumBullet(count);
            GameData.DateSave();
            this.showMagazineData();
        }
        if (TipsController.Instance.IsPack && !this._isClick) {
            this._isClick = true;
            TipsController.Instance.nextTips2D(3);
        }
    }

    //#region 子弹包
    @property(Label)
    CountLabel: Label = null;

    HaveBulletCount: number = 0;
    private _bullet: Bullet = null;

    addBulletTs(bullet: Bullet) {
        this.Name = bullet.Name;
        this._bullet = bullet;
        this.showBulletCount();
    }

    subNumBullet(num: number = 1) {
        const isExist: boolean = GameData.subBulletByName(this.Name, num);
        if (!isExist) {
            Backpack.Instance.showBullet();
            return;
        }
        this.showBulletCount();
    }

    showBulletCount() {
        this.HaveBulletCount = this._bullet.Count;
        this.CountLabel.string = this._bullet.Count.toString();
    }

}


